'use strict';

const gulp = require('gulp'),
    clean = require('gulp-clean'),
    cssmin = require('gulp-cssmin'),
    rename = require('gulp-rename'),
    sass = require('gulp-sass'),
    sourcemaps = require('gulp-sourcemaps'),
    concat = require('gulp-concat'),
    uglify = require('gulp-uglify'),
    babel = require('gulp-babel'),
    pump = require('pump');

const vender = [
    'js/popper.js',
    'js/popper-utils.js',
    'js/tooltip.js',
    'js/popover.js',
    'js/dropdown.js',
    'js/alert.js',
    'js/button.js',
    'js/carousel.js',
    'js/collapse.js',
    'js/modal.js',
    'js/scrollspy.js',
    'js/tab.js',
    'js/util.js',
    'js/bootstrap-datepicker.js',
    'js/bootstrap-datepicker.zh-CN.js',
];

gulp.task('clean', function () {
    return gulp.src('dist/css/*.css')
        .pipe(clean({
            force: true
        }));
});

gulp.task('sass', function () {
    return gulp.src('scss/royalblue-theme-mobile.scss')
        .pipe(sass({
            indentWidth: 4,
            outputStyle: 'expanded'
        }))
        .pipe(gulp.dest('dist/css'));
});

gulp.task('cssmin', function () {
    return gulp.src('dist/css/royalblue-theme-mobile.css')
        .pipe(sourcemaps.init())
        .pipe(cssmin())
        .pipe(rename({
            suffix: '.min'
        }))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('dist/css'));
});

gulp.task('default', ['sass', 'cssmin']);

gulp.task('jsconcat', function (cb) {
    pump([
        gulp.src(vender),
        babel({
            presets: [
                ['es2015', {
                    loose: true,
                    modules: false,
                    exclude: ['transform-es2015-typeof-symbol']
                }]
            ],
            plugins: ['transform-es2015-modules-strip',
                'transform-object-rest-spread'
            ]
        }),
        concat('royalblue-theme-mobile.js'),
        gulp.dest('dist/js')
    ], cb);
});

gulp.task('jsmin', function (cb) {
    pump([
        gulp.src('dist/js/royalblue-theme-mobile.js'),
        sourcemaps.init(),
        uglify(),
        rename({
            suffix: '.min'
        }),
        sourcemaps.write('.'),
        gulp.dest('dist/js')
    ], cb);
});

gulp.task('watch', function () {
    gulp.watch('scss/*.scss', ['default']);
});